package com.lsfb.signln.View;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.ajguan.library.IRefreshHeader;
import com.ajguan.library.State;
import com.lsfb.signln.R;

/**
 * Created by Administrator on 2018/2/26 0026.
 */

public class SignRefreshHeadView extends FrameLayout implements IRefreshHeader {
    private TextView textView;
    private View arrowIcon;
    private View successIcon;
    private View loadingIcon;
    private Animation rotate_up;
    private Animation rotate_down;
    private Animation rotate_infinite;

    public SignRefreshHeadView(@NonNull Context context) {
        super(context);

        // 初始化动画
        rotate_up = AnimationUtils.loadAnimation(context, com.ajguan.library.R.anim.rotate_up);
        rotate_down = AnimationUtils.loadAnimation(context, com.ajguan.library.R.anim.rotate_down);
        rotate_infinite = AnimationUtils.loadAnimation(context, com.ajguan.library.R.anim.rotate_infinite);

        inflate(context, R.layout.default_refresh_header, this);
        textView = (TextView) findViewById(com.ajguan.library.R.id.text);
        arrowIcon = findViewById(com.ajguan.library.R.id.arrowIcon);
        successIcon = findViewById(com.ajguan.library.R.id.successIcon);
        loadingIcon = findViewById(com.ajguan.library.R.id.loadingIcon);
    }

    @Override
    public void reset() {
        textView.setText(getResources().getText(com.ajguan.library.R.string.header_reset));
        successIcon.setVisibility(GONE);
        arrowIcon.setVisibility(VISIBLE);
        arrowIcon.clearAnimation();
        loadingIcon.setVisibility(INVISIBLE);
        loadingIcon.clearAnimation();
    }

    @Override
    public void pull() {

    }

    @Override
    public void refreshing() {
        arrowIcon.setVisibility(VISIBLE);
        loadingIcon.setVisibility(VISIBLE);
        textView.setText(getResources().getText(com.ajguan.library.R.string.header_refreshing));
//        textView.setText("刷新中");
        arrowIcon.clearAnimation();
        loadingIcon.startAnimation(rotate_infinite);
    }

    private boolean firstshow = false;

    @Override
    public void onPositionChange(float currentPos, float lastPos, float refreshPos, boolean isTouch, State state) {
        // 往上拉
        if (currentPos > 0) {
            //拉出来了
            if (!firstshow) {
                firstshow = true;
            }

        } else {
            //缩进去了
            firstshow = false;
        }
        if (currentPos < refreshPos && lastPos >= refreshPos) {
            Log.i("", ">>>>up");
            if (isTouch && state == State.PULL) {
                textView.setText(getResources().getText(com.ajguan.library.R.string.header_pull));
//                textView.setText("下拉刷新");

                arrowIcon.clearAnimation();
                arrowIcon.startAnimation(rotate_down);
            }
            // 往下拉
        } else if (currentPos > refreshPos && lastPos <= refreshPos) {
            Log.i("", ">>>>down");
            if (isTouch && state == State.PULL) {
                textView.setText(getResources().getText(com.ajguan.library.R.string.header_pull_over));
//                textView.setText("松开刷新");

                arrowIcon.clearAnimation();
                arrowIcon.startAnimation(rotate_up);
            }
        }
    }

    @Override
    public void complete() {
        arrowIcon.setVisibility(GONE);
        loadingIcon.setVisibility(INVISIBLE);
        loadingIcon.clearAnimation();
        successIcon.setVisibility(GONE); //不要成功的图标
        textView.setText(getResources().getText(com.ajguan.library.R.string.header_completed));
//        textView.setText("刷新成功");
    }
}
